package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @Author:石小俊
 * @Date:2022/12/13 15:45
 * @Version:1.0-SNAPSHOT
 * @Description: 用户登录
 * 判断用户所输入的用户名与密码是否正确
 * 如果正确
 *  将当前输入的用户名与密码保存到cookie中,便于下次直接访问
 *  页面跳转到主页
 * 如果不正确
 *  页面仍然返回登录页面,并在登录功能下方提示:用户名或密码错误
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        // 获取用户输入的用户名与密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 判断用户输入的用户名与密码是否正确
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8","root","");
            conn.setAutoCommit(false);

            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where username = ? ")
                    .append(" and password = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                // 用户输入的用户名与密码正确
                // 将用户名与密码保存到Cookie中
                // 数据保存一天
                CookieUtil.addCookie("username",username,86400,response);
                CookieUtil.addCookie("password",password,86400,response);
                // 页面跳转到主页
                request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);
                return;
            }
            // 用户名或密码错误
            request.setAttribute("loginMsg","用户名或密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);

            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
